﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class CellStylingDemo : Window, IWindow
    {
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion
        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }

        public CellStylingDemo()
        {
            InitializeComponent();

            PrepareGridData();
            dataGrid.ItemsSource = listSaleRecord;

            dataGrid.RowsHierarchy.Visible = false;

            this.ApplyCellStyles();

            dataGrid.ColumnsHierarchy.AllowDragDrop = false;
            dataGrid.ColumnsHierarchy.AllowResize = false;
            dataGrid.AllowContextMenuSorting = true;
        }

        private void ApplyCellStyles()
        {
            Style orangeStyle1 = this.Resources["OrangeCellStyle1"] as Style;
            Style orangeStyle2 = this.Resources["OrangeCellStyle2"] as Style;
            Style orangeStyle3 = this.Resources["OrangeCellStyle3"] as Style;
            Style redStyle1 = this.Resources["RedCellStyle1"] as Style;
            Style redStyle2 = this.Resources["RedCellStyle2"] as Style;
            Style redStyle3 = this.Resources["RedCellStyle3"] as Style;
            Style blueStyle1 = this.Resources["BlueCellStyle1"] as Style;
            Style blueStyle2 = this.Resources["BlueCellStyle2"] as Style;
            Style blueStyle3 = this.Resources["BlueCellStyle3"] as Style;
            Style blueStyle4 = this.Resources["BlueCellStyle4"] as Style;
            Style greenStyle1 = this.Resources["GreenCellStyle1"] as Style;
            Style greenStyle2 = this.Resources["GreenCellStyle2"] as Style;
            Style greenStyle3 = this.Resources["GreenCellStyle3"] as Style;
            Style greenStyle4 = this.Resources["GreenCellStyle4"] as Style;

            for (int i = 0; i < this.dataGrid.RowsHierarchy.Items.Count; i++)
            {
                HierarchyItem row = this.dataGrid.RowsHierarchy.Items[i];
                for (int j = 0; j < this.dataGrid.ColumnsHierarchy.Items.Count; j++)
                {
                    HierarchyItem column = this.dataGrid.ColumnsHierarchy.Items[j];

                    object value = this.dataGrid.CellsArea.GetCellValue(row, column);

                    if (column.Text == "Options Granted")
                    {
                        if ((int)value <= 1000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, orangeStyle1);
                        }
                        else if ((int)value > 1000 && (int)value <= 5000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, orangeStyle2);
                        }
                        else if ((int)value > 5000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, orangeStyle3);
                        }
                    }
                    else if (column.Text == "Bonus")
                    {
                        if ((int)value <= 5000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle1);
                        }
                        else if ((int)value > 5000 && (int)value <= 8000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle2);
                        }
                        else if ((int)value > 8000 && (int)value <= 10000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle3);
                        }

                        else if ((int)value > 10000 && (int)value <= 20000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle4);
                        }
                        else if ((int)value > 20000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, orangeStyle3);
                        }
                    }
                    else if (column.Text == "Salary")
                    {
                        if ((int)value <= 20000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle1);
                        }
                        else if ((int)value > 20000 && (int)value <= 40000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle2);
                        }
                        else if ((int)value > 40000 && (int)value <= 60000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle3);
                        }
                        else if ((int)value > 60000 && (int)value <= 70000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle4);
                        }
                        else if ((int)value > 60000 && (int)value <= 70000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, greenStyle4);
                        }
                        else if ((int)value > 70000 && (int)value <= 80000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, orangeStyle3);
                        }
                        else if ((int)value > 80000 && (int)value <= 100000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, redStyle1);
                        }
                        else if ((int)value > 100000 && (int)value <= 125000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, redStyle2);
                        }
                        else if ((int)value > 125000)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, redStyle3);
                        }
                    }
                    else if (column.Text == "Months since hired")
                    {
                        if ((int)value <= 10)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, blueStyle1);
                        }
                        else if ((int)value > 10 && (int)value <= 30)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, blueStyle2);
                        }
                        else if ((int)value > 30 && (int)value <= 70)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, blueStyle3);
                        }
                        else if ((int)value > 70 && (int)value <= 100)
                        {
                            this.dataGrid.CellsArea.SetCellStyle(row, column, blueStyle4);
                        }
                    }
                }
            }
        }

        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Michael",
                    "Guylène"
                };

        string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Saavedra",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Peterson",
                    "Winkler",
                    "Bein",
                    "Petersen",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Nodier"
                };

        static string[,] productNames = new string[,] 
                {
                    {"Black Tea", "1.95"},
                    {"Green Tea", "1.95"},
                    {"Caffè Espresso", "1.45"},
                    {"Doubleshot Espresso", "1.75"},
                    {"Caffè Latte", "2.25"},
                    {"White Chocolate Mocha", "2.35"},
                    {"Caramel Latte", "2.35"},
                    {"Caffè Americano", "1.65"},
                    {"Cappuccino", "2.10"},
                    {"Espresso Truffle", "2.45"},
                    {"Espresso con Panna", "1.81"},
                    {"Peppermint Mocha Twist", "1.99"}
                };

        public class EmployeeRecord
        {
            public EmployeeRecord(string Name, int Months, int Salary, int Bonus, int Options)
            {
                this._name = Name;
                this._months = Months;
                this._salary = Salary;
                this._bonus = Bonus;
                this._options = Options;
            }

            #region Private Members
            private string _name;
            private int _months;
            private int _salary;
            private int _bonus;
            private int _options;
            #endregion

            #region Properties

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public int Months
            {
                get { return _months; }
                set { _months = value; }
            }
            public int Bonus
            {
                get { return _bonus; }
                set { _bonus = value; }
            }

            public int Salary
            {
                get { return _salary; }
                set { _salary = value; }
            }

            public int Options
            {
                get { return _options; }
                set { _options = value; }
            }

            #endregion
        }


        #endregion

        public List<EmployeeRecord> listSaleRecord = new List<EmployeeRecord>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 1000;
            listSaleRecord.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                listSaleRecord.Add(new EmployeeRecord(
                    string.Format("{0} {1}",
                    firstNames[rand.Next(0, firstNames.Length - 1)],
                    lastNames[rand.Next(0, lastNames.Length - 1)]),
                    rand.Next(1, 100), rand.Next(50000, 150000),
                    rand.Next(1000, 20000), rand.Next(0, 5500)
                    ));
            }
        }

        #endregion

        // Custom class implements the IValueConverter interface.
        public class ProductIdToNameConverter : IValueConverter
        {
            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if ((int)value >= productNames.Length / 2)
                    return "";

                return productNames[(int)value, 0];
            }

            public object ConvertBack(object value, Type targetType,
                object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

            #endregion
        }

        protected override void OnClosing(CancelEventArgs e)
        {

            base.OnClosing(e);
            this.dataGrid.Dispose();
            this.dataGrid = null;
        }
    }


    public class DoubleToCurrencyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return string.Format("{0:C}", (double)value);
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, System.Globalization.CultureInfo culture)
        {
            return double.Parse(value.ToString(), System.Globalization.NumberStyles.Currency);
        }
    }

    


}
